﻿using AIVOnline.PathModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp_mysqldb_sample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 
            testDB();
        }

        private void testDB()
        {
            //var contextTmp = new AIVOnlineDbContext("server=localhost;port=3306;uid=root;pwd=root;database=aivonline;Persist Security Info=True;");
            var contextTmp = new AIVOnline_MySql_DbContext();// AIVOnlineDbContext();//

            //创建数据库，如果不存在。
            //bool bCreate = contextTmp.Database.CreateIfNotExists();
            //Console.WriteLine($"try create database: {bCreate}");

            AIVNodeState a = null;
            AIVNodeState nodeState = contextTmp.AIVNodeStates.Where<AIVNodeState>(o => o.NodeId == 1).FirstOrDefault();
            if (null == nodeState)
            {
                //插入一行值
                nodeState = new AIVNodeState() { NodeId = 2, Owner = "AGV1" };
                nodeState.State = NodeState.Normal;
                a = contextTmp.AIVNodeStates.Add(nodeState);
            }
            else
            {
                //
                if (nodeState.State != NodeState.Occupied)
                {
                    nodeState.State = NodeState.Occupied;
                }
                else
                {
                    nodeState.State = NodeState.Normal;
                }
            }


            // 保存改变
            int nResult = contextTmp.SaveChanges();

            Console.WriteLine($"try add node: {nResult}");
        }
    }
}
